package com.example.medical_delivery_system.service.impl;

import com.example.medical_delivery_system.common.ServiceResultEnum;
import com.example.medical_delivery_system.dao.AdminUserMapper;
import com.example.medical_delivery_system.entity.User;
import com.example.medical_delivery_system.service.AdminUserService;
import com.example.medical_delivery_system.util.PageQueryUtil;
import com.example.medical_delivery_system.util.PageResult;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdminUserServiceImpl implements AdminUserService {

    private AdminUserMapper adminUserMapper;


    @Override
    public PageResult getUserPage(PageQueryUtil pageUtil) {
        List<User> users = adminUserMapper.findUserList(pageUtil);
        int total = adminUserMapper.getTotalUsers(pageUtil);
        PageResult pageResult = new PageResult(users, total, pageUtil.getLimit(), pageUtil.getPage());
        return pageResult;
    }

    @Override
    public String saveUser(User user) {
        if (adminUserMapper.insertSelective(user) > 0) {
            return ServiceResultEnum.SUCCESS.getResult();
        }
        return ServiceResultEnum.DB_ERROR.getResult();
    }

    @Override
    public String updateUser(User user) {
        User temp = adminUserMapper.selectByPrimaryKey(user.getUserId());
        if (temp == null) {
            return ServiceResultEnum.DATA_NOT_EXIST.getResult();
        }
        temp.setUserName(user.getUserName());
        temp.setUserPassword(user.getUserPassword());
        temp.setUserType(user.getUserType());
        if (adminUserMapper.updateByPrimaryKeySelective(temp) > 0) {
            return ServiceResultEnum.SUCCESS.getResult();
        }
        return ServiceResultEnum.DB_ERROR.getResult();
    }

    @Override
    public User getUserById(Long id) {
        return adminUserMapper.selectByPrimaryKey(id);
    }

    @Override
    public Boolean deleteBatch(Long[] ids) {
        if (ids.length < 1) {
            return false;
        }
        //删除数据
        return adminUserMapper.deleteBatch(ids) > 0;
    }
}
